//
//  ZTabBarController.m
//  SchoolOrder
//
//  Created by 朱圳 on 2017/11/21.
//  Copyright © 2017年 朱圳. All rights reserved.
//

/******************    TabBar          *************/
#define MallClassKey   @"rootVCClassString"
#define MallTitleKey   @"title"
#define MallImgKey     @"imageName"
#define MallSelImgKey  @"selectedImageName"

#import "ZTabBarController.h"
#import "ZTabBar.h"
#import "BaseNavigationController.h"
#import "UserViewController.h"
#import "OrderCourseViewController.h"
#import "HomeViewController.h"
#import "YHclockVC.h"

@interface ZTabBarController ()<UITabBarControllerDelegate>
@end

@implementation ZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTabBar];
    [self addDcChildViewContorller];
    self.delegate = self;

}

-(void)setUpTabBar
{
    ZTabBar *tabBar = [[ZTabBar alloc] init];
    tabBar.backgroundColor = [UIColor whiteColor];
    
    //KVC把系统换成自定义
    [self setValue:tabBar forKey:@"tabBar"];
}
#pragma mark - 添加子控制器
- (void)addDcChildViewContorller
{
    NSArray *childArray = @[
                            @{MallClassKey  : @"HomeViewController",
                              MallTitleKey  : @"首页",
                              MallImgKey    : @"首页",
                              MallSelImgKey : @"首页"},
                            
                            @{MallClassKey  : @"YHclockVC",
                              MallTitleKey  : @"今日打卡",
                              MallImgKey    : @"stu-index-icon1",
                              MallSelImgKey : @"stu-index-icon1"},
                            
                            @{MallClassKey  : @"OrderCourseViewController",
                              MallTitleKey  : @"我的约课",
                              MallImgKey    : @"stu-person-t2",
                              MallSelImgKey : @"stu-person-t2"},
                            
                            @{MallClassKey  : @"UserViewController",
                              MallTitleKey  : @"个人中心",
                              MallImgKey    : @"stu-index-icon3",
                              MallSelImgKey : @"stu-index-icon3"},
                            
                            ];
    [childArray enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIViewController *vc = [NSClassFromString(dict[MallClassKey]) new];
        vc.navigationItem.title = dict[MallTitleKey];
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
        UITabBarItem *item = nav.tabBarItem;
        item.title = dict[MallTitleKey];
        item.image = [UIImage imageNamed:dict[MallImgKey]];
        item.selectedImage = [[UIImage imageNamed:dict[MallSelImgKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.imageInsets = UIEdgeInsetsMake(0, 0,0, 0);//（当只有图片的时候）需要自动调整
        
      [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName, [UIFont systemFontOfSize:10.0],NSFontAttributeName,nil] forState:UIControlStateSelected];
        
        [self addChildViewController:nav];
        
    }];
}

#pragma mark - <UITabBarControllerDelegate>
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    //点击tabBarItem动画
    [self tabBarButtonClick:[self getTabBarButton]];
    
}
- (UIControl *)getTabBarButton{
    NSMutableArray *tabBarButtons = [[NSMutableArray alloc]initWithCapacity:0];
    
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            [tabBarButtons addObject:tabBarButton];
        }
    }
    UIControl *tabBarButton = [tabBarButtons objectAtIndex:self.selectedIndex];
    
    return tabBarButton;
}
#pragma mark - 点击动画
- (void)tabBarButtonClick:(UIControl *)tabBarButton
{
    for (UIView *imageView in tabBarButton.subviews) {
        if ([imageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
            //需要实现的帧动画,这里根据自己需求改动
            CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
            animation.keyPath = @"transform.scale";
            animation.values = @[@1.0,@1.1,@0.9,@1.0];
            animation.duration = 0.5;
            animation.calculationMode = kCAAnimationCubic;
            //添加动画
            [imageView.layer addAnimation:animation forKey:nil];
        }
    }
}


#pragma mark - 禁止屏幕旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;//只支持这一个方向(正常的方向)
}
@end
